/*
 * $Id$
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <egpd.h>

int debug;
int foreground;
struct egpd_config *conf;

int
main(int argc, char *argv[])
{
   char *config = NULL;
   int retval = 0;

#if __OpenBSD__ || __NetBSD__
   setproctitle("%d", getpid());
#endif
   signal(SIGTERM, handle_signal);
   signal(SIGHUP, handle_signal);
   signal(SIGINT, handle_signal);
   signal(SIGPIPE, handle_signal);

   debug = 0;
   foreground = 0;

   if (argc <= 1)
      usage();
   else {
      int ch = 0;
      while ((ch = getopt(argc, argv, "dfhc:")) != EOF) {
         switch (ch) {
         case 'c':
            config = optarg;
            break;

         case 'd':
            debug = 1;
            break;

         case 'f':
            foreground = 1;
            break;

         case 'h':
            usage();
            break;

         default:
            usage();
            break;
         }
      }

      initvars();
      if (config) {
         if (parse_config(config))
            exit(1);

         egp_socket();
      }
      cleanup();
   }

   return retval;
}

__dead void
usage(void)
{
   fprintf(stderr, "usage: %s [ -dfh ] [ -c config]\n", __progname);
   exit(1);
}
